﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rabbit.Model;

namespace Rabbit.BusinessLogicLayer
{
    public class UserStatusBL : AutoUpdateMessageClassBase<Status>
    {
        public long UserID { get; set; }
        
        public UserStatusBL():base(double.MaxValue)
        {

        }

        private Statuses GetStatusList(long cursorID)
        {
            string statusString = RequestHelper.SendOAuthRequest(
                string.Format("statuses/user_timeline/{0}.json", UserID), "cursor=" + cursorID + "&count=" + Utils.Configuration.PageSize);

            Statuses statuses = WrappedJsonHelper.Deserialize<Statuses>(statusString);
            return statuses;
        }
        protected override void FetchNewMessage()
        {
            throw new NotImplementedException();
        }

        protected override void FetchMessage()
        {
            throw new NotImplementedException();
        }

        public override List<Status> GetNextPageMessage()
        {
            Statuses tempStatuses = GetStatusList(NextPageCursorID);

            //push previouse page cursor id to the queue
            CursorQueue.Push(CurrentPageCursorID.Value);
            CurrentPageCursorID = NextPageCursorID;

            //set next page cursor id to the returned value
            NextPageCursorID = tempStatuses.CursorID;
            FetchedMessageCount = tempStatuses.StatusList.Count;
            return tempStatuses.StatusList == null ? new List<Status>() : tempStatuses.StatusList;
        }

        public override List<Status> GetPreviousePageMessage()
        {
            if (CursorQueue.Count == 0)
            {
                return null;
            }

            CurrentPageCursorID = CursorQueue.Pop();

            Statuses tempStatuses = GetStatusList(CurrentPageCursorID.Value);
            NextPageCursorID = tempStatuses.CursorID;
            FetchedMessageCount = tempStatuses.StatusList.Count;
            return tempStatuses.StatusList == null ? new List<Status>() : tempStatuses.StatusList;
        }

        public override List<Status> GetFirstPageMessage()
        {
            CursorQueue.Clear();

            Statuses tempStatuses = GetStatusList(-1);
            NextPageCursorID = tempStatuses.CursorID;
            CurrentPageCursorID = -1;
            return tempStatuses.StatusList==null?new List<Status>():tempStatuses.StatusList;
        }
    }
}
